﻿#include "GUIGeometryNodeDialog.h"
#include "ui_GUIGeometryNodeDialog.h"

#include "GUIFrame/MainWindow.h"

#include "FITK_Kernel/FITKCore/FITKActionOperator.h"
#include "OperatorsInterface/GraphEventOperator.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsGeometryData.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoInterfaceFactory.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelPoint.h"

#include "PickDataProvider/GUIPickInfo.h"

#include <QMessageBox>
#include <QTableWidgetItem>
#include <QSpacerItem>

namespace GUI {

    GUIGeometryNodeDialog::GUIGeometryNodeDialog(Core::FITKActionOperator* oper, QWidget* parent) : GUIGeometryBaseDialog(nullptr, oper, parent),
        _ui(new Ui::GUIGeometryNodeDialog)
    {
        _ui->setupUi(this);

        this->createGeometryObj(Interface::FITKGeoEnum::FITKGeometryComType::FGTPoint);

        this->init();
    }

    GUIGeometryNodeDialog::GUIGeometryNodeDialog(Interface::FITKAbsGeoModelPoint* obj, Core::FITKActionOperator* oper, QWidget* parent) : GUIGeometryBaseDialog(obj, oper, parent),
        _ui(new Ui::GUIGeometryNodeDialog)
    {
        _ui->setupUi(this);

        this->init();
    }

    GUIGeometryNodeDialog::~GUIGeometryNodeDialog()
    {
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }
    }

    void GUIGeometryNodeDialog::init()
    {
        Interface::FITKAbsGeoModelPoint* geoData = dynamic_cast<Interface::FITKAbsGeoModelPoint*>(_obj);
        Acoustics::FITKAcousticsGeometryData* geometryData = FITKAPP->getGlobalData()->getGeometryData<Acoustics::FITKAcousticsGeometryData>();
        if (!geometryData || !geoData) return;
        //获取数据
        QString name = geometryData->checkName(tr("Point-1"));
        if (!_isCreate) name = _obj->getDataObjectName();
        double x = geoData->x();
        double y = geoData->y();
        double z = geoData->z();
        //设置界面
        _ui->lineEdit_Name->setText(name);
        _ui->lineEdit_PointX->setText(QString::number(x));
        _ui->lineEdit_PointY->setText(QString::number(y));
        _ui->lineEdit_PointZ->setText(QString::number(z));
    }

    void GUIGeometryNodeDialog::on_pushButton_Cancel_clicked()
    {
        _oper->setArgs("objID", -1);
        this->reject();
    }

    void GUIGeometryNodeDialog::on_pushButton_OK_clicked()
    {
        if (_isCreate)
        {
            if (!this->createObjData())
                return;
        }
        else
        {
            if (!this->editObjData())
                return;
        }
        if (_oper && _obj) {
            _oper->setArgs("objID", _obj->getDataObjectID());
        }
        this->accept();
    }

    void GUIGeometryNodeDialog::getDataFormWidget()
    {
        Interface::FITKAbsGeoModelPoint* geoData = dynamic_cast<Interface::FITKAbsGeoModelPoint*>(_obj);
        if (!geoData) return;
        //获取数据
        double x = _ui->lineEdit_PointX->text().toDouble();
        double y = _ui->lineEdit_PointY->text().toDouble();
        double z = _ui->lineEdit_PointZ->text().toDouble();
        //设置数据
        geoData->setX(x);
        geoData->setY(y);
        geoData->setZ(z);
    }

    QString GUIGeometryNodeDialog::getNameText()
    {
        return _ui->lineEdit_Name->text();
    }
}
